package org.hxyjs.dao.impl;

import org.hxyjs.dao.OrderdetailsDao;
import org.hxyjs.entity.Orderdetails;
import org.hxyjs.entity.Orderdetails;
import org.hxyjs.entity.Orderdetails;
import org.hxyjs.mapper.OrderdetailsMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("orderdetailsDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class OrderdetailsDaoImpl implements OrderdetailsDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private OrderdetailsMapper orderdetailsMapper;
    public List<Orderdetails>  getAllOrderdetailss() throws Exception{
        return orderdetailsMapper.selectByExample(null);
    }

    @Override
    public Integer deleteOrderdetailsByOdno(String odno)  throws Exception{
        return orderdetailsMapper.deleteByPrimaryKey(odno);
    }

    @Override
    public Integer updateOrderdetailsByOdno(Orderdetails orderdetails)  throws Exception{
        return orderdetailsMapper.updateByPrimaryKey(orderdetails);
    }

    @Override
    public Integer addOrderdetails(Orderdetails orderdetails)  throws Exception{
        orderdetails.setOdno(initId());
        return orderdetailsMapper.insertSelective(orderdetails);
    }
    public String initId(){
        String maxOdno = orderdetailsMapper.getMaxOdno();
        return GenNumTools.initId("",3,maxOdno);
    }

    @Override
    public List<Orderdetails> getOrderdetailsByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)orderdetailsMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return orderdetailsMapper.getOrderdetailsByPage(map);
    }
}
